package com.leilei;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

/**
 * @author lei
 * @version 1.0
 * @date 2021/3/11 21:09
 * @desc flink 1.12中 sink操作 输出到文件
 */
public class FlinkSink2_WriteFile {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        List<User> users = Arrays.asList(new User("张三", 12),
                new User("李四", 13), new User("王五", 14));
        DataStreamSource<User> dataStreamSource = env.fromCollection(users);
        String filePath = "E:\\aa\\flink-learn-1.12\\flink-learn-4-sink\\src\\main\\resources\\file";

        dataStreamSource.writeAsText(filePath, FileSystem.WriteMode.OVERWRITE)
                .setParallelism(1);
        env.execute();
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    @Builder
    public static class User {
        private String name;
        private Integer age;
    }
}
